package cn.ymkj.controller;

import cn.ymkj.pojo.SysJobPo;
import cn.ymkj.service.SysJobPOService;
import cn.ymkj.task.CronTaskRegistrar;
import cn.ymkj.thread.SchedulingRunnable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
public class JobController {

    @Resource
    private SysJobPOService sysJobPOService;
    @Resource
    private CronTaskRegistrar cronTaskRegistrar;

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @PostMapping("/job")
    @ResponseBody
    public String job(SysJobPo sysJob){
        sysJob.setCreateTime(new Date());
        sysJobPOService.add(sysJob);
        SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
        cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
        return "新增成功";
    }
}
